package org.springframework.core.type.classreading;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.asm.AnnotationVisitor;
import org.springframework.asm.ClassWriter;
import org.springframework.asm.Label;
import org.springframework.asm.MethodVisitor;
import org.springframework.asm.Opcodes;
import org.springframework.asm.SpringAsmInfo;
import org.springframework.asm.Type;
import org.springframework.asm.signature.SignatureReader;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.ParameterMetadata;
import org.springframework.core.type.ServiceMethodMetadata;


public class ServiceMethodMetadataReadingVisitor extends MethodVisitor implements ServiceMethodMetadata{

	private final int access;

	private final String name, desc, signature, declaringClassName;
	private final String[] exceptions;
	private final ClassLoader classLoader;
	protected final ServiceClassMetadataReadingVisitor classVisitor;
	private final Map<String, AnnotationAttributes> attributeMap = new LinkedHashMap<String, AnnotationAttributes>(2);

	protected Type returnType;
	protected final StandardParameterMetadata[] parameters;
	protected final List<String> typeVariables = new ArrayList<String>();
	protected boolean isArrayReturnType;
	@Override
	public Type getReturnType() {
		return returnType;
	}
	@Override
	public boolean isArrayReturnType() {
		return isArrayReturnType;
	}
	@Override
	public String getDesc() {
		return desc;
	}
	
	@Override
	public ParameterMetadata[] getParameters(){
		return parameters;
	}
	
	@Override
	public MethodVisitor buildMethod(ClassWriter cw){
		return cw.visitMethod(Opcodes.ACC_PUBLIC, name, desc, signature, exceptions);
	}
	
	public ServiceMethodMetadataReadingVisitor(ServiceClassMetadataReadingVisitor classVisitor, 
			int access, String name, String desc, String signature, String[] exceptions, MethodVisitor mv) {
		super(SpringAsmInfo.ASM_VERSION, mv);
		this.name = name;
		this.access = access;
		this.desc = desc;
		this.signature = signature;
		this.exceptions = exceptions;
		this.classVisitor = classVisitor;
		this.classLoader = classVisitor.getClassLoader();
		this.declaringClassName = classVisitor.getClassName();
		Type type = Type.getType(desc);
		returnType = type.getReturnType();
		Type[] ps = type.getArgumentTypes();
		int len = ps.length;
		parameters = new StandardParameterMetadata[len];
		for(int i = 0; i < len; i++){
			parameters[i] = new StandardParameterMetadata(i, ps[i]);
		}
		if(null != signature){
			SignatureReader sr = new SignatureReader(signature);
			sr.accept(new ServiceSignatureReadingVisitor(this));
		}
	}
	
	@Override
	public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
		StandardParameterMetadata spm = parameters[parameter];
		spm.annotationDesc = desc;
		spm.annotationVisible = visible;
		return spm;
	}
	
	@Override
	public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
		String className = Type.getType(desc).getClassName();
		return new AnnotationAttributesReadingVisitor(className, this.attributeMap, null, this.classLoader);
	}
	private Map<Integer, String> methodParameters =  new HashMap<Integer, String>();
	@Override
	public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
	    methodParameters.put(index, name);
	    super.visitLocalVariable(name, desc, signature, start, end, index);
	}
	
	public String getParameterName(int index){
		return methodParameters.get(index);
	}

	@Override
	public String getMethodName() {
		return name;
	}

	@Override
	public String getDeclaringClassName() {
		return declaringClassName;
	}

	@Override
	public boolean isStatic() {
		return ((this.access & Opcodes.ACC_STATIC) != 0);
	}

	@Override
	public boolean isFinal() {
		return ((this.access & Opcodes.ACC_FINAL) != 0);
	}

	@Override
	public boolean isOverridable() {
		return (!isStatic() && !isFinal() && ((this.access & Opcodes.ACC_PRIVATE) == 0));
	}

	@Override
	public boolean isAnnotated(String annotationType) {
		return this.attributeMap.containsKey(annotationType);
	}
	
	@Override
	public AnnotationAttributes getAnnotationAttributes(String annotationType) {
		return this.attributeMap.get(annotationType);
	}

}
